﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

namespace HQWeb.Procurement
{
    public partial class InventoryList : BasePage
    {

        private static string strType;
        protected void Page_Load(object sender, EventArgs e)
        {
           VerdictIsLogin(); 
            if (!IsPostBack)
            {
                string strId = Request["pid"];
                if (strId=="all")
                {
                    strId = "0";
                }
                strType = "'" + strId + "'" + ",";
                GetAllType(strId);
                PIdHid.Value = strType.Substring(0, strType.Length -1);
                
                BindDropDownList();
                if (Convert.ToBoolean(Session["IsAll"]) == false)
                {
                    string strBID = Session["BRANCH_ID"].ToString();
                    if (Session["Branch_id"] == null)
                    {
                        Session["Branch_id"] = strBID;
                    }
                    Branch_Drop.SelectedValue = strBID;
                    int Branch_id = int.Parse(Branch_Drop.SelectedValue);
                    BindLibraryName(Branch_id);
                }
                else
                {
                    ListItem item = new ListItem("全部", "-1");
                    item.Selected = true;
                    ddl_library.Items.Add(item);
                    if (Session["Branch_id"] == null)
                    {
                        Session["Branch_id"] = "-1";
                    }
                    if (Session["Library_id"] == null)
                    {
                        Session["Library_id"] = "-1";
                    }
                }
                if (Session["Branch_id"] != null)
                {
                    Branch_Drop.SelectedValue = Session["Branch_id"].ToString();
                    int Branch_id = int.Parse(Branch_Drop.SelectedValue);
                    BindLibraryName(Branch_id);
                }
                if (Session["Library_id"] != null)
                {
                    ddl_library.SelectedValue = Session["Library_id"].ToString();
                }
                BindDate();
            }  
        }

        #region 绑定数据
        private void BindDate()
        {
            int bid ;
            int.TryParse(Session["Branch_id"].ToString(), out bid);

            int lid;
            int.TryParse(Session["Library_id"].ToString(), out lid);
            string typeid = PIdHid.Value;
            string titel = this.txtName.Text.Trim();
            DataSet LibraryMaterials = new DataSet();
            BLL.LibrarySys.T_PUB_LIBRARYMATERIAL bll = new BLL.LibrarySys.T_PUB_LIBRARYMATERIAL();
            LibraryMaterials = bll.GetMaterialList(bid, lid, titel, typeid);
            GridView1.DataSource = LibraryMaterials.Tables[0];
            GridView1.DataBind();
        }
        #endregion

        #region 获得所有下级类型
        private void GetAllType(string strID)
        {
            GetChild(strID);
        }
        #endregion

        #region 获取孩子节点
        private void GetChild(string strID)
        {
            BLL.SysManage.T_PUB_MATERIALTYPE bll = new BLL.SysManage.T_PUB_MATERIALTYPE();
            List<Model.SysManage.T_PUB_MATERIALTYPE> modelist = new List<Model.SysManage.T_PUB_MATERIALTYPE>();
            int intid = 0;
            int.TryParse(strID, out intid);
            string strWhere = string.Format(" PARENT_ID = {0}", intid);
            modelist = bll.GetModelList(strWhere);
            foreach (Model.SysManage.T_PUB_MATERIALTYPE item in modelist)
            {
                strType += "'" + item.TYPE_ID + "'" + ",";
                   GetChild(item.TYPE_ID.ToString());
            }
        }
        #endregion

        #region 绑定分店列表
        private void BindDropDownList()
        {
            BLL.SysManage.T_PUB_BRANCH LibraryBll = new BLL.SysManage.T_PUB_BRANCH();
            IList il = LibraryBll.GetModelList("");
            Branch_Drop.ClearSelection();
            Branch_Drop.DataSource = il;
            Branch_Drop.DataTextField = "BRANCH_NAME"; //dropdownlist的Text的字段 
            Branch_Drop.DataValueField = "BRANCH_ID";//dropdownlist的Value的字段 
            Branch_Drop.DataBind();
            ListItem item = new ListItem("全部", "-1");
            item.Selected = true;
            Branch_Drop.Items.Add(item);
        }
        #endregion

        #region 绑定仓库
        private void BindLibraryName(int Branch_id)
        {
            BLL.SysManage.T_PUB_LIBRARY bll = new BLL.SysManage.T_PUB_LIBRARY();
            IList librarylist = bll.GetModelByBranch_id(Branch_id);
            if (librarylist != null)
            {
                ddl_library.ClearSelection();
                ddl_library.DataSource = librarylist;
                ddl_library.DataTextField = "LIBRARY_NAME";
                ddl_library.DataValueField = "LIBRARY_ID";
                ddl_library.DataBind();
                ListItem item = new ListItem("全部", "-1");
                if (Session["Library_id"] == null)
                {
                    item.Selected = true;
                    Session["Library_id"] = "-1";
                }
                ddl_library.Items.Add(item);
            }
        }
        #endregion

        #region 翻页事件
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                //strWhere = strWhere.Replace("order by operatetime DESC", "");
                int newPageIndex = 0;

                if (-2 == e.NewPageIndex)
                { // when click the "GO" Button
                    TextBox txtNewPageIndex = null;
                    GridViewRow pagerRow = GridView1.Controls[0].Controls[GridView1.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                    //GridViewRow pagerRow = GridView1.BottomPagerRow; //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                    if (null != pagerRow)
                    {
                        txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                    }

                    if (null != txtNewPageIndex)
                    {
                        newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                    }

                    newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
                    newPageIndex = newPageIndex >= GridView1.PageCount ? GridView1.PageCount - 1 : newPageIndex;
                    GridView1.PageIndex = newPageIndex;
                }
                else if (e.NewPageIndex == -1)
                {
                    newPageIndex = 0;
                    GridView1.PageIndex = 0;
                }
                else
                {  // when click the first, last, previous and next Button
                    newPageIndex = e.NewPageIndex;
                    GridView1.PageIndex = e.NewPageIndex;
                }

                // check to prevent form the NewPageIndex out of the range


                BindDate();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 查看删除

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strCommand = e.CommandName;
            string strID = e.CommandArgument.ToString();
            if (strCommand.Equals("EditData"))  //编辑
            {
                Response.Redirect("../LibarySys/PurchaseInLibrary.aspx?mid=" + strID);
            }
            else if(strCommand.Equals("btnDelete"))
            {
                 Response.Redirect("../LibarySys/ScrapApply.aspx?mid=" + strID);
            }
            
        }
        #endregion

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int index = e.Row.RowIndex;
                DataRowView drv = (DataRowView)e.Row.DataItem;
                string strState = drv.Row["FLAG"].ToString();
                LinkButton btn2 = (LinkButton)e.Row.Cells[5].FindControl("btnEdit");
                LinkButton btn1 = (LinkButton)e.Row.Cells[5].FindControl("btnDelete");
                if (strState == "2")
                {
                    btn2.Text = "采购入库";
                    btn2.CommandName = "EditData";
                    btn1.Text="报废申请";
                    btn1.CommandName = "btnDelete";
                }
                else
                {
                    btn2.Text = "";
                    btn2.Enabled = false;
                    btn1.Text = "";
                    btn1.Enabled = false;
                }
            }
        }

        protected void tbnSearch_Click(object sender, EventArgs e)
        {
            BindDate();
        }

        protected void Branch_Drop_SelectedIndexChanged(object sender, EventArgs e)
        {
            int Branch_id = int.Parse(Branch_Drop.SelectedValue);
            Session["Branch_id"] = Branch_id.ToString();
            BindLibraryName(Branch_id);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("PurchaseInLibrary.aspx");
        }

        protected void ddl_library_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["Library_id"] = ddl_library.SelectedValue;
        }
    }
}